(function($){
	
    $(function(){
    	
    	$('.timepicker')[0] && $('.timepicker').ptTimeSelect();
    	$('.delete-time').live('click',function(e){
    		$(this).parent().remove();
    	});
    	
		//添加中奖用户
        $('#addAwardUser').dialog({
            autoOpen: false,
            height: 300,
            width: 420,
            modal: true,
            buttons: {
                '确定': function(){
                	//ajax方法
                	var awardId =  $('#awardId').val();
        			var passport =  $('#username').val();
        			var activeId = $('#activeId').val()
        			var num =  $('#num').val();
        			$.ajax({
                		type : "POST",
                		url : ctx+"/award/ajaxAddAssignWinner.do",
                		data : "awardId="+awardId+"&assignPassport="+passport+"&assignNum="+num+"&activeId="+activeId,
                		success : function(msg){
                			
                			if(msg.startWith("error:")){
		            			  showTips("添加中奖用户出错了");
		           			  }else{
		           				showTips("执行成功",function(){
		           				$('#addAwardUser').dialog('close');
		           					
		           				});
		           				msg = eval("("+msg+")");
	           					var addID = msg.id;
	           					var passport = msg.passport;
	           					var maxNum = msg.maxNum;
	           					$(document).find('tbody').append('<tr class="odd">\
										<td>'+addID+'</td>\
										<td>'+passport+'</td>\
										<td>'+maxNum+'</td>\
										<td><a href="javascript:void(0);" class="is-delete">删除</a></td>\
											</tr>');
		           			  }
                		}
                	});
                }
            }
        });
                	
                	
                	
        $('.add-award-user-link').click(function(e){
            e.preventDefault();
            $('#addAwardUser').dialog('open');
        });
        //保存按钮点击
        $('#addAwardConfigButton').click(function(e){
        	if($('#probabilityCheckBox').attr('checked')==='checked'){
        		if($("#prizeNumRadio").attr('checked')==='checked'){
        			if($("#prizeNumInput").val()==''){
        				showTips("尾数中奖数值不能为空！");
        				return ;
        			}
        		}
        	}
        	//保存
        	var time= '';
        	var timeCount=0;
        	$('.timepicker', '.datetime').each( function () {
        		if(!($.trim($(this).val())===''||$(this).val()===undefined)){
        			time =time+ $(this).val()+';';
        			timeCount = timeCount+1;
        		}
        	})
        	if(timeCount>=57){
				showTips('添加时间超过56个');
			}else{
				var lastSemicolonIndex =  time.lastIndexOf(';');
				if(lastSemicolonIndex!==-1){
					time = time.substring(0, lastSemicolonIndex);
				}
	        	
	        	//js获取时间中奖的时间然后拼成串hidden一下，submit
	        	$('#timeHidden').val(time);
	        	//添加检验框架
	        	var flag = $("#addsavrAwardConfigChanceForm").validate(VALIDATE.saveAwardConfigChance).form();
	        	if(flag){
	        		$('#addsavrAwardConfigChanceForm').submit();
	        	}
	        	
			}
        	
        	
        });
        //点击添加文本框
        $('#addTime').click(function(e){
        	var dom = 	'<p>\
        					<span>中奖时间：</span>\
        					<input type="text" class="timepicker" placeholder="请选择开始时间" readonly="readonly"/>\
        					<input type="button" class="delete-time" value="删除">\
        				</p>';
        	
        	$('.datetime').append( dom);	//执行完后，已经生成了html
        	$('.datetime').find('.timepicker').last().ptTimeSelect();
        });
        
        //删除指定用户
        $('.is-delete').live('click', function(e){
        	var assWinnerId = $(this).parent().parent().find('td').first().text();
        	var parent  =$(this).parent().parent();
        	var activeId = $('#activeId').val()
        	
        	$.ajax({
        		type : "POST",
        		url : ctx+"/award/ajaxDeleteOneAddAssignWinner.do",
        		data : "assignedWinnerId="+assWinnerId+"&activeId="+activeId,
        		success : function(msg){
	        			if(msg.startWith("error:")){
	        				showTips("删除指定用户出错");
	        			}else{
	        				showTips("执行成功");
	        				parent.remove();
	        			}
        			}
        	
        		});
        });
        //概率中奖选中好未选中效果处理
        $('#probabilityCheckBox').click(function(e){
        	checkChecked();
        });
        
        checkChecked();
        
        
       
    });
    function checkChecked(){
    	if($('#probabilityCheckBox').attr('checked')==='checked'){
    		$('#prizeNumRadio').attr('disabled',false);
        	$('#prizeTimeRadio').attr('disabled',false);
        	$('#prizeNumInput').attr('disabled',false);
        	$('.timepicker').attr('disabled',false);
        	$('#addTime').attr('disabled',false);
        	$('.delete-time').attr('disabled',false);
    	}else{
    		$('#prizeNumRadio').attr('disabled',true);
        	$('#prizeTimeRadio').attr('disabled',true);
        	$('#prizeNumInput').attr('disabled',true);
        	$('.timepicker').attr('disabled',true);
        	$('#addTime').attr('disabled','disabled');	
        	$('.delete-time').attr('disabled','disabled');
    	}
    }

})(jQuery);